package com.lost.octopus.es.helper.entity.query;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author zhangbh
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageOut implements Serializable {

    private static final long serialVersionUID = -3208016707405241915L;
    private int currentPage;
    private int linesPerPage;
    private long totalNum;
    private int totalPage;

    public PageOut(int currentPage, int linesPerPage, long totalNum) {
        this.currentPage = currentPage;
        this.linesPerPage = linesPerPage;
        this.totalNum = totalNum;
        autoTotalPage();
    }

    public PageOut(PageIn in, long totalNum) {
        this.currentPage = in.getCurrentPage();
        this.linesPerPage = in.getLinesPerPage();
        this.totalNum = totalNum;
        autoTotalPage();
    }

    /**
     * 自动计算总页码冗余字段
     */
    private void autoTotalPage() {
        if (linesPerPage == 0) {
            this.totalPage = 0;
        } else {
            this.totalPage = Math.toIntExact((totalNum + linesPerPage - 1) / linesPerPage);
        }
    }
}
